﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using FCNS.KeepMoney.Data;
using FCNS.KeepMoneyLibrary.Data;
using FCNS.KeepMoney.Util;

using BrightIdeasSoftware;
namespace FCNS.KeepMoney.Forms
{
    public partial class RegularEntrysForm : FCNS.KeepMoney.Forms.ParentForm
    {
        public RegularEntrysForm()
        {
            InitializeComponent();
            Program.Translation.ApplyTo(this);
        }

        List<RegularEntryField> regularEntryFields = null;
        List<InterestField> interestFields = null;
        List<BankField> bankFields = null;
        List<GroupField> groupFields = null;
        ComboBox comboBox = new ComboBox();
        bool save = false;

        public void Init()
        {
            FCNS.Utility.BannerFactory.CreateBannerEx(this, pictureBox1, Properties.Resources.m4_32,
               LanguageUtil.定期账务, LanguageUtil.分期付款_定期收益);

            HotItemStyleUtil.SetHotItemStyle(listViewEntry, false);
            listViewEntry.AddDecoration(new EditingCellBorderDecoration(true));

            labelError.Text = string.Empty;
            regularEntryFields = RegularEntryField.GetRegularEntryField;
            interestFields = InterestField.GetInterestField;
            bankFields = BankField.GetBankField.FindAll(bf => { return bf.Enable; });
            groupFields = GroupField.GetGroupField;

            LoadEntrys();

            comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        private void LoadEntrys()
        {
            if (!checkBoxFinish.Checked)
                listViewEntry.SetObjects(regularEntryFields.FindAll(f=>{return f.Finish != f.columnTime;}));
            else
                listViewEntry.SetObjects(regularEntryFields);
        }


        private void checkBoxFinish_CheckedChanged(object sender, EventArgs e)
        {
            LoadEntrys();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (listViewEntry.SelectedObject == null)
                return;

            if (MessageBox.Show(LanguageUtil.此操作将移除已生成的定期账务记录, LanguageUtil.操作不可逆_是否继续,
                MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
                return;

            RegularEntryField ef = (RegularEntryField)listViewEntry.SelectedObject;
            ef.Remove();
            LoadEntrys();

            save = true;
        }

        private void buttonNew_Click(object sender, EventArgs e)
        {
            RegularEntryField f = new RegularEntryField(null);
            f.columnStartDate = DateTime.Now;
            f.AddRowToDataTable();
            f.GroupId = groupFields[0].Id;
            f.BankId = bankFields[0].Id;
            f.InterestId = interestFields[0].Id;

            LoadEntrys();

            save= true;
        }

        private void OnFormClosing(object sender, FormClosingEventArgs e)
        {
            if (!save)
                return;

            Program.SaveKMD = true;
           // CalcData.CalcRegular();
            Program.TheMainForm.ReLoadEntry();
        }

        private void buttonInterest_Click(object sender, EventArgs e)
        {
            InterestForm f = new InterestForm();
            f.Init();
            f.ShowDialog();

            interestFields = InterestField.GetInterestField;
        }


        private void dataListView1_CellEditStarting(object sender, BrightIdeasSoftware.CellEditEventArgs e)
        {
            labelError.Text = string.Empty;
            RegularEntryField field = (RegularEntryField)e.RowObject;
            if (field.Finish > 0)//不能修改已生成定期账务记录的数据
            {
                e.Cancel = true;
                labelError.Text = LanguageUtil.此行已有使用记录_无法编辑;
                return;
            }
            switch (e.Column.AspectName)
            {
                case "columnDay":
                    ((NumericUpDown)e.Control).Maximum = 31;
                    ((NumericUpDown)e.Control).Minimum = 1;
                    break;

                case "columnBankName":
                    comboBox.Items.Clear();
                    foreach (BankField bf in bankFields)
                        comboBox.Items.Add(bf.Name);

                    comboBox.Location = e.Control.Location;
                    comboBox.SelectedIndex = 0;
                    comboBox.Size = e.Control.Size;
                    e.Control = comboBox;
                    break;

                case "columnGroupName":
                    comboBox.Items.Clear();
                    foreach (GroupField gf in groupFields)
                        comboBox.Items.Add(gf.Name);

                    comboBox.Location = e.Control.Location;
                    comboBox.SelectedIndex = 0;
                    comboBox.Size = e.Control.Size;
                    e.Control = comboBox;
                    break;

                case "columnInterestName":
                    comboBox.Items.Clear();
                    foreach (InterestField inf in interestFields)
                        comboBox.Items.Add(inf.columnName);

                    comboBox.Location = e.Control.Location;
                    comboBox.SelectedIndex = 0;
                    comboBox.Size = e.Control.Size;
                    e.Control = comboBox;
                    break;
            }
        }

        private void dataListView1_CellEditFinishing(object sender, BrightIdeasSoftware.CellEditEventArgs e)
        {
            RegularEntryField field = (RegularEntryField)e.RowObject;
            switch (e.Column.AspectName)
            {
                case "columnStartDate":
                    field.columnStartDate = (DateTime)e.NewValue;
                    break;
                case "columnName":
                    field.columnName = (string)e.NewValue;
                    break;
                case "columnTime":
                    field.columnTime = (int)e.NewValue;
                    break;
                case "columnDay":
                    field.columnDay = (int)e.NewValue;
                    break;
                case "columnMoney":
                    decimal de = 0;
                    decimal.TryParse((string)e.NewValue, out de);
                    if (de < 0)
                        de = 0;

                    field.columnMoney = de;
                    break;
                case "columnInterestName":
                    field.InterestId = interestFields[comboBox.SelectedIndex].Id;
                    e.NewValue = comboBox.Text;
                    break;
                case "columnGroupName":
                    field.GroupId = groupFields[comboBox.SelectedIndex].Id;
                    e.NewValue = comboBox.Text;
                    break;
                case "columnBankName":
                    field.BankId = bankFields[comboBox.SelectedIndex].Id;
                    e.NewValue = comboBox.Text;
                    break;
            }
            save = true;
        }

        private void dataListView1_SubItemChecking(object sender, BrightIdeasSoftware.SubItemCheckingEventArgs e)
        {
            RegularEntryField field = (RegularEntryField)e.RowObject;
            if (field.Finish > 0)//不能修改已生成定期账务记录的数据
            {
                e.Canceled = true;
                labelError.Text = LanguageUtil.此行已有使用记录_无法编辑;
                return;
            }
        }
    }
}